package com.wzy.vx.controller;

import com.lly835.bestpay.model.PayResponse;
import com.wzy.vx.DTO.OrderDTO;
import com.wzy.vx.enums.ResuletEnum;
import com.wzy.vx.exception.SellException;
import com.wzy.vx.service.OrderService;
import com.wzy.vx.service.PayService;
import com.wzy.vx.service.impl.PayServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * @ProjectName: vx
 * @Package: com.wzy.vx.controller
 * @ClassName: PayController
 * @Author: Administrator
 * @Description: ${description}
 * @Date: 2019/3/16 13:03
 * @Version: 1.0
 */
@Controller
@RequestMapping(value = "/pay")
public class PayController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private PayService payService;

    /**
     * 创建订单号
     * */
    @GetMapping("/create")
    public  ModelAndView  create(@RequestParam("orderId") String orderId,
                         @RequestParam("returnUrl") String returnUrl,
                         Map<String, Object> map){
        OrderDTO orderDTO = orderService.findOrder(orderId);
        //1.查询订单状态
        if (orderDTO ==null){
            throw  new SellException(ResuletEnum.ORDER_NOT_EXIST);
        }
        //2.支付
        PayResponse payResponse = payService.create(orderDTO);

        map.put("payResponse", payResponse);
        map.put("returnUrl", returnUrl);

        return new ModelAndView("pay/create", map);
    }

    /**
     * 支付后 异步通知
     * */
    @GetMapping("/notify")
    public  ModelAndView  notify(@RequestBody String notifyData){
        payService.notify(notifyData);
        //返回给微信处理结果
        return new ModelAndView("pay/success");
    }



}
